package c.x.jy.admin.config;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.EnableWebMvcConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.unit.DataSize;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
@SuppressWarnings("all")
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public class MyMvcConfig    implements WebMvcConfigurer  {
	 
  
	 
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
		 
            "classpath:/static/" , };
 
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// TODO Auto-generated method stub  .jsp
		 // WebMvcAutoConfiguration.DEFAULT_PREFIX="";
		//registry.addViewController("/hello").setViewName("/helloPage");
		//registry.addViewController("/index").setViewName("forward:/static/index.html");  //vue配置
	     registry.addViewController("/").setViewName("forward:/index.html");
	//     registry.addViewController("/index").setViewName("index.html");
		/*
		 * // registry.addViewController("/index").setViewName("/index.html");
		 * registry.addViewController("/system/menu").setViewName("/system/menu");
		 * registry.addViewController("/store/store_account").setViewName("/system/user"
		 * ); registry.addViewController("/sys/title").setViewName("title/title");
		 * registry.addViewController("/sys/article").setViewName("cms/article");
		 * 
		 * registry.addViewController("/sys/category").setViewName("title/category");
		 * registry.addViewController("/sys/cmsCategory").setViewName("cms/cmsCategory")
		 * ;
		 */
	 //   registry.addViewController("/WEB-INF/views").setViewName("title/title"); 
	    
	    registry.setOrder(Ordered.HIGHEST_PRECEDENCE); 
		WebMvcConfigurer.super.addViewControllers(registry);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		  // 解决静态资源无法访问
		//   registry.addResourceHandler("/resources/*").addResourceLocations("classpath:/resources/")   
       //s .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
		   registry.addResourceHandler("/**").addResourceLocations("classpath:/static/" ) .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS)  ;
	 	WebMvcConfigurer.super.addResourceHandlers(registry);
	}
	
 
	
	
	 @Override
	    public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
	        converters.add(getMappingJackson2HttpMessageConverter());
	    }
	 
	 

	    /**
	     * JSON全局日期转换器
	     */
	    @Bean
	    public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
	        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
	        //设置日期格式
	        ObjectMapper objectMapper = new ObjectMapper();
	        objectMapper.setDateFormat(CustomDateFormat.instance);
	        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
	        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
	        //设置中文编码格式
	        List<MediaType> list = new ArrayList<MediaType>();
	        list.add(MediaType.APPLICATION_JSON_UTF8);
	        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
	        return mappingJackson2HttpMessageConverter;
	    }

/*	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**")
                .addResourceLocations("classpath:/imgs/");
        
        super.addResourceHandlers(registry);
    }*/
	    
	    
	    @Bean
	    public MultipartConfigElement multipartConfigElement() {
	        MultipartConfigFactory factory = new MultipartConfigFactory();
	        //单个文件最大 5M
	       factory.setMaxFileSize(DataSize.ofBytes(20*1024*1024)); //KB,MB
	        /// 设置总上传数据总大小
	       factory.setMaxRequestSize(DataSize.ofBytes(20*1024*1024));
	     
	        return factory.createMultipartConfig();
	    }
	    
	    @Bean("multipartResolver")
	    public MultipartResolver feignFormEncoder() { 
	    	CommonsMultipartResolver c=	  new CommonsMultipartResolver() ;
	    	c.setDefaultEncoding("UTF-8");
	    	c.setMaxUploadSize(20*1024*1024);
			return c; 
	    	} 
	    
	    /**
	     * 添加跨域 配置
	     * @param registry
	    
	    @Override
		public void addCorsMappings(CorsRegistry registry) {
	        registry.addMapping("/**")// 允许跨域访问的路径
	                .allowedOrigins("*")// 允许跨域访问的源
	                .allowCredentials(true)// 是否发送cookie
	                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许请求方法
                 .allowedHeaders("*")  // 允许头部设置
	                .maxAge(3600);// 预检间隔时间
	    }
	    
 */
@Bean
public FilterRegistrationBean corsFilter() {
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  CorsConfiguration config = new CorsConfiguration();
  config.setAllowCredentials(true);
  config.addAllowedOrigin("*");
  config.addAllowedHeader("*");
  config.addAllowedMethod("*");
  source.registerCorsConfiguration("/**", config);
  FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
  bean.setOrder(0);
  return bean;

}
 
}
